package com.xy.blog.user.listener;

import com.xy.blog.user.service.event.UserDefaultOpenAccountEvent;
import com.xy.blog.user.service.impl.AccountServiceImpl;
import com.xy.blog.user.service.impl.UserServiceImpl;
import com.xy.blog.user.service.model.AccountBO;
import com.xy.blog.user.service.model.UserBO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @author zanhonglei
 * @version V1.0
 * @Description:
 * @date 2020/3/14
 */
@Component
public class AccountServiceListener {

    @Autowired
    private AccountServiceImpl accountServiceImpl;

    @Autowired
    private UserServiceImpl userServiceImpl;

    @Async
    @EventListener
    public void defaultOpenAccount(UserDefaultOpenAccountEvent event) {
        Long userId = event.getUserId();
        Long defaultAttrDictId = event.getDefaultAttrDictId();
        AccountBO accountBO = new AccountBO();
        accountBO.setUserId(userId);
        UserBO userById = userServiceImpl.getUserById(userId);
        String attrValue = userById.getAttrDictMaps().get(defaultAttrDictId);
        accountBO.setAccount(attrValue);
        accountBO.setPassword("123456");
        accountServiceImpl.save(accountBO);
    }

}
